#include "../String/SString.hpp"

#include <cstring>
#include <fstream>
#include <iostream>
#include <iterator>

using namespace std;

void question1()
{
    SStringC proceesor;
    SString str;
    proceesor.strAssign(str, "2468_WE_ARE_APPRECIATE_BBNO$");

    int values[36] = { 0 };
    char* temp = str.ch;
    while (*temp != '\0') {
        if (*temp >= 'A' && *temp <= 'Z') {
            values[(*temp) - 'A']++;
        } else if (*temp >= '0' && *temp <= '9') {
            values[(*temp) - '0' + 26]++;
        }
        temp++;
    }

    ofstream file("question1.txt");
    if (!file.is_open()) {
        cerr << "打开文件失败！" << endl;
        return;
    }

    for (int i = 0; i < 26; ++i) {
        if (values[i] > 0) {
            file << char('A' + i) << ": " << values[i] << endl;
        }
    }

    for (int i = 0; i < 10; ++i) {
        if (values[i + 26] > 0)
            file << char('0' + i) << ": " << values[i + 26] << endl;
    }

    file.close();
    cout << "文件保存成功！" << endl;
}

void question2Reversed(char* s, int left, int right);

void question2()
{
    SString str;
    SStringC proceesor;
    proceesor.strAssign(str, "ABCDefGhiJK");

    cout << "原始字符串: " << str.ch << endl;

    question2Reversed(str.ch, 0, str.length - 1);

    cout << "逆序字符串：" << str.ch << endl;
}

void question2Reversed(char* s, int left, int right)
{
    if (left >= right)
        return;
    char temp = s[left];
    s[left] = s[right];
    s[right] = temp;
    question2Reversed(s, left + 1, right - 1);
}

void question3_insert(char* s, char* t, int pos)
{
    int s_len = 0, t_len = 0;
    char *p = s, *q = t;
    while (*p != '\0'){
        s_len++;
        p++;
    }
    while (*q != '\0'){
        t_len++;
        q++;
    }
    for (int i=s_len; i>=pos; i--) {
        s[i+t_len] = s[i];
    }
    for (int j=0; j<t_len;j++){
        s[pos+j] = t[j];
    }
}

void question3()
{
    SString s;
    SString t;
    SStringC proceesor;
    proceesor.strAssign(s, "ISpeed");
    proceesor.strAssign(t, "Show");

    cout << "原始字符串s: " << s.ch << " | 长度: " << s.length << endl;
    cout << "原始字符串t: " << t.ch << " | 长度: " << t.length << endl;
    int pos = 1;
    if (pos < 0 || pos > s.length)
        return;
    question3_insert(s.ch, t.ch, 1);

    cout << "插入后字符串s: " << s.ch << " | 长度: " << strlen(s.ch) << endl;
}

int main()
{
    question3();
    return 0;
}
